
 
from pydantic import BaseModel,FilePath,Field
from langchain_core.utils.pydantic import (
    PydanticBaseModel,
    TBaseModel,
)

from langchain_core.beta.runnables.context import Context
from langchain_core.runnables.passthrough import RunnablePassthrough
from langchain_core.prompts.prompt import PromptTemplate
from langchain_core.output_parsers.string import StrOutputParser
from langchain.schema import HumanMessage, SystemMessage
 
 
from langchain.chat_models import ChatOpenAI
from langchain.output_parsers import PandasDataFrameOutputParser, OutputFixingParser
from langchain_core.prompts import PromptTemplate
import pandas as pd

from langchain_core.example_selectors import (
    LengthBasedExampleSelector,
    MaxMarginalRelevanceExampleSelector,
    SemanticSimilarityExampleSelector,
)
from langchain_core.prompts import (
    AIMessagePromptTemplate,
    BaseChatPromptTemplate,
    BasePromptTemplate,
    ChatMessagePromptTemplate,
    ChatPromptTemplate,
    FewShotChatMessagePromptTemplate,
    FewShotPromptTemplate,
    FewShotPromptWithTemplates,
    HumanMessagePromptTemplate,
    MessagesPlaceholder,
    PipelinePromptTemplate,
    PromptTemplate,
    StringPromptTemplate,
    SystemMessagePromptTemplate,
    load_prompt,
)

from langchain._api import create_importer
from langchain.prompts.prompt import Prompt

llm = ChatOpenAI(
    model="deepseek-chat",
    temperature=0,
    openai_api_key="sk-605e60a1301040759a821b6b677556fb",
    base_url="https://api.deepseek.com/v1")

from langchain_core.prompts import SystemMessagePromptTemplate

from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder

from langchain.prompts import FewShotPromptTemplate, PromptTemplate

from langchain.prompts import FewShotPromptTemplate, PromptTemplate







'''
# 1. 定义单条示例模板
example_template = """
输入: {input}
输出: {output}
"""
example_prompt = PromptTemplate(
    input_variables=["input", "output"],
    template=example_template
)

# 2. 准备示例数据
examples = [
    {"input": "2+2", "output": "4"},
    {"input": "2+3", "output": "5"}
]

# 3. 构建FewShotPromptTemplate
few_shot_prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="你是一个数学助手，根据示例回答问题：",
    suffix="输入: {user_input}\n输出:",
    input_variables=["user_input"]
)

# 使用示例
print(few_shot_prompt.format(user_input="3+3"))



from langchain_core.prompts import ChatPromptTemplate



template = ChatPromptTemplate(
[
("system", "You are a helpful AI bot. Your name is {name}."),
("human", "Hello, how are you doing?"),
("ai", "I'm doing well, thanks!"),
("human", "{user_input}"),
]
)

prompt_value = template.invoke(
{
"name": "Bob",
"user_input": "What is your name?",
}
)

print(prompt_value)


examples = [
    {"question": "1+1等于几", "answer": "数学问题，请联系张老师"},
    {"question": "足球的英文", "answer": "英语问题，请联系李老师"}
]
example_prompt = PromptTemplate(
    input_variables=["question", "answer"],
    template="问题: {question}\n回答: {answer}"
)

prompt = FewShotPromptTemplate(
    examples=examples,
    example_prompt=example_prompt,
    prefix="根据问题类型选择对应老师：",
    suffix="问题:回答:",
    
)

rs=prompt.format()

print(rs)


prompt = ChatPromptTemplate.from_messages([
    ("system", "你是{role}助手"),
    MessagesPlaceholder(variable_name="history"),  # 历史消息插入点
    ("human", "{input}")
])

from langchain_core.messages import HumanMessage, AIMessage

formatted_prompt = prompt.format(
    role="技术顾问",
    history=[
        HumanMessage(content="如何用Python连接MySQL?"),
        AIMessage(content="使用PyMySQL或SQLAlchemy...")
    ],
    input="请给出PyMySQL示例代码"
)


print(formatted_prompt)


# 定义系统指令模板
template = "你是一个专注{domain}的AI助手，请遵守以下规则：\n{rules}"
system_prompt = SystemMessagePromptTemplate.from_template(template)

# 填充生成系统消息
formatted_msg = system_prompt.format(
    domain="机器学习",
    rules="1. 回答需包含代码示例\n2. 避免主观猜测"
)


print(formatted_msg)


examples = [
    {"input": "happy", "output": "sad"},
    {"input": "tall", "output": "short"}
]
example_prompt = PromptTemplate(
    input_variables=["input","output"],
    template="Input: {input}\nOutput: {output}"
)
selector = LengthBasedExampleSelector(
    examples=examples,
    example_prompt=example_prompt,
    max_length=25
)

dynamic_prompt = FewShotPromptTemplate(
    example_selector=selector,
    example_prompt=example_prompt,
    prefix="生成反义词：",
    suffix="Input: {word}\nOutput:",
    input_variables=["word"]
)
 
'''

 